home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / NEW_TECH / UPC2S1.ZIP / UUCICO / dcptpkt.c < prev    next >
C/C++ Source or Header  |  1993-09-23  |  8KB  |  232 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       d c p t p k t . c                                            */
  3. /*                                                                    */
  4. /*       UUCP 't' protocol support                                    */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: dcptpkt.c 1.4 1993/09/24 03:43:27 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: dcptpkt.c $
  24.  * Revision 1.4  1993/09/24  03:43:27  ahd
  25.  * Correct byte reordering functions
  26.  *
  27.  * Revision 1.3  1993/09/21  01:42:13  ahd
  28.  * Delete functions duplicated from dcpgpkt.c
  29.  *
  30.  * Revision 1.2  1993/09/20  04:48:25  ahd
  31.  * TCP/IP support from Dave Watt
  32.  * 't' protocol support
  33.  * OS/2 2.x support (BC++ 1.0 for OS/2)
  34.  *
  35.  * Revision 1.1  1993/09/18  19:47:24  ahd
  36.  * Initial revision
  37.  *
  38.  */
  39.  
  40. /*--------------------------------------------------------------------*/
  41. /*       TCP/IP ("t") protocol.                                       */
  42. /*                                                                    */
  43. /*       Protocol for over reliable (TCP/IP) paths.                   */
  44. /*                                                                    */
  45. /*       't' procotol is done by simply transmitting the four byte    */
  46. /*       length of the packet in network byte order (big-endian)      */
  47. /*       followed by the packet data itself.  No padding is           */
  48. /*       performed.                                                   */
  49. /*                                                                    */
  50. /*       Note:  Many of the functions (msg write, msg read, start     */
  51. /*       of file, file eof) for this protocol are as the same as      */
  52. /*       the functions for 'g' protocol, and we use the actual 'g'    */
  53. /*       protocol copies as defined in dcpsys.c.                      */
  54. /*--------------------------------------------------------------------*/
  55.  
  56. /*--------------------------------------------------------------------*/
  57. /*                        System include files                        */
  58. /*--------------------------------------------------------------------*/
  59.  
  60. #include <stdio.h>
  61. #include <time.h>
  62. #include <string.h>
  63.  
  64. #if defined(WIN32) || defined(_Windows)
  65. #include "winsock.h"       // Needed for byte ordering
  66. #endif
  67.  
  68. /*--------------------------------------------------------------------*/
  69. /*                    UUPC/extended include files                     */
  70. /*--------------------------------------------------------------------*/
  71.  
  72. #include "lib.h"
  73. #include "dcp.h"
  74. #include "dcptpkt.h"
  75. #include "dcpsys.h"
  76. #include "hostable.h"
  77. #include "security.h"
  78. #include "ssleep.h"
  79. #include "modem.h"
  80. #include "commlib.h"
  81.  
  82. #ifdef _Windows
  83. #include "pwinsock.h"
  84. #endif
  85.  
  86. #ifndef _WINSOCKAPI_
  87.  
  88. /*--------------------------------------------------------------------*/
  89. /*     Network functions needed when no winsock functions available   */
  90. /*--------------------------------------------------------------------*/
  91.  
  92. static unsigned long htonl( const unsigned long input );
  93. static unsigned long ntohl( const unsigned long input );
  94.  
  95. /*--------------------------------------------------------------------*/
  96. /*       h t o n l                                                    */
  97. /*                                                                    */
  98. /*       Convert unsigned long from host to network byte order        */
  99. /*--------------------------------------------------------------------*/
  100.  
  101. static unsigned long htonl( const unsigned long input )
  102. {
  103.    unsigned long result;
  104.    unsigned char *p = (unsigned char *) &result;
  105.    int i;
  106.  
  107.    for (i = 0 ; i < sizeof input; i++ )
  108.       p[3 - i] = (unsigned char) ((input >> (i*8)) & 0xff);
  109.  
  110.    printmsg(15,"htonl: %lx = %x %x %x %x",input, p[0], p[1], p[2], p[3] );
  111.  
  112.    return result;
  113.  
  114. } /* htonl */
  115.  
  116. /*--------------------------------------------------------------------*/
  117. /*       n t o h l                                                    */
  118. /*                                                                    */
  119. /*       Convert unsigned long from network to host byte order        */
  120. /*--------------------------------------------------------------------*/
  121.  
  122. static unsigned long ntohl( const unsigned long input )
  123. {
  124.    unsigned char *p = (unsigned char *) &input;
  125.    unsigned long result = 0;
  126.    int i;
  127.  
  128.    for (i = 0 ; i < sizeof input; i++ )
  129.       result = (result << 8) + p[i];
  130.  
  131.    printmsg(15,"ntonh: %x %x %x %x = %lx",p[0], p[1], p[2], p[3], result );
  132.  
  133.    return result;
  134.  
  135. } /* ntohl */
  136.  
  137. #endif
  138.  
  139. /*--------------------------------------------------------------------*/
  140. /*    t o p e n p k                                                   */
  141. /*                                                                    */
  142. /*    Open "t" protocol to other system                               */
  143. /*--------------------------------------------------------------------*/
  144.  
  145. #ifdef __TURBOC__
  146. #pragma argsused
  147. #endif
  148.  
  149. short topenpk(const boolean master)
  150. {
  151.    s_pktsize = r_pktsize = 1024;    // Fixed for 't' procotol
  152.  
  153.    return DCP_OK;
  154.  
  155. } /* topenpk */
  156.  
  157. /*--------------------------------------------------------------------*/
  158. /*    t g e t p k t                                                   */
  159. /*                                                                    */
  160. /*    Receive an "t" protocol packet of data from the other system    */
  161. /*--------------------------------------------------------------------*/
  162.  
  163. short tgetpkt(char *packet, short *bytes)
  164. {
  165.    unsigned short recv;
  166.    unsigned long nrecv;
  167.  
  168.    if (sread( (char *) &nrecv, sizeof nrecv, M_tPacketTimeout) < sizeof nrecv)
  169.    {
  170.       printmsg(0,"tgetpkt: Length read failed");
  171.       return -1;
  172.    }
  173.  
  174.    recv = (short) ntohl( nrecv );
  175.  
  176.    if ( recv > r_pktsize )
  177.    {
  178.       printmsg(0,"tgetpkt: Buffer overrun!  Wanted %d bytes, %d queued",
  179.                   (int) r_pktsize,
  180.                   (int) recv );
  181.       return -1;
  182.    }
  183.  
  184.    if (sread( packet, recv, M_tPacketTimeout) < recv)
  185.    {
  186.       printmsg(0,"tgetpkt: Data read failed for %d bytes", (int) recv);
  187.       return -1;
  188.    }
  189.  
  190.    remote_stats.packets++;
  191.  
  192.    *bytes = recv;
  193.  
  194.    return 0;
  195.  
  196. } /* tgetpkt */
  197.  
  198. /*--------------------------------------------------------------------*/
  199. /*    t s e n d p k t                                                 */
  200. /*                                                                    */
  201. /*    Send an "t" protocol packet to the other system                 */
  202. /*--------------------------------------------------------------------*/
  203.  
  204. short tsendpkt(char *ip, short len)
  205. {
  206.  
  207.    unsigned long nxmit = htonl((unsigned long) len);
  208.  
  209.    if ( swrite( (char *) &nxmit, sizeof nxmit ) != sizeof nxmit )
  210.       return -1;
  211.  
  212.    if ( len && (swrite( ip , len ) != len ))
  213.       return -1;
  214.  
  215.    remote_stats.packets++;
  216.  
  217.    return 0;
  218.  
  219. } /* tsendpkt */
  220.  
  221.  
  222. /*--------------------------------------------------------------------*/
  223. /*    t c l o s e p k                                                 */
  224. /*                                                                    */
  225. /*    Shutdown "t" procotol with other system                         */
  226. /*--------------------------------------------------------------------*/
  227.  
  228. shor